﻿using System;
using System.Drawing;
using FireStarterModeller.BO;
using FireStarterModeller.Test.BO;
using FireStarterModeller.UI;
using Habanero.Base.Exceptions;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Mapper
{
    [TestFixture]
// ReSharper disable ClassWithVirtualMembersNeverInherited.Global
    public class TestSortPropertySelectorMapper
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
        }

        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();
        }
/*
        private static IControlFactory CreateControlFactory()
        {
            return new ControlFactoryWin();
        }*/
        
        // Construct
        //  public MyClass(IControlHabanero ctl, string propName, bool isReadOnly, IControlFactory factory) : base(ctl, propName, isReadOnly, factory)
        // Inherits from ControlMapper

        // Maps to single Relationship only
        // not Prop
        // not Multiple Relationship
        // Single Relationship BOs must be of type DMSortOrder
        // Test when BO is null
        // Test when related SortOrder is null
        // Test when related sort order changes from/to null
        // Test when Sort Order is not null
        // Test Control Value changes, should update the relationship (most prob when the value changes from null to something)

        // DMSortOrder
        // - Has many SortProperties

        //What is a DMSortProperty
        // - has Property, Sort Direction, Ordinal(Order)
        
        // On Control (not mapper)
        // - Set PropertySourceClass
        // Has TextBox with tostring of DMSortOrder ('Prop1 ASC, Prop2 DESC ...')
        // Has a '...' button which open the SortOrderEditorForm

        // SortOrderEditorForm
        // - Set PropertySourceClass
        //   - Populates the ComboBoxColumn source in the Grid
        // Has an EditableGrid with a SortProp and a SortOrder Column
        // SortOrder Column can be a checkbox for ascending or a Combo with the Lookup Values 'Ascending, Descending'
        // Doesn't have a close button in the title, is not sizeable
        // Has an OK and Cancel button
        // Can do ordinal positioning using the 'up' and 'down' buttons next to the grid (like properties grid)



        private static IControlFactory GetControlFactory()
        {
            ControlFactoryWin factory = new ControlFactoryWin();
            GlobalUIRegistry.ControlFactory = factory;
            return factory;
        }

        // Construct
        //  public MyClass(IControlHabanero ctl, string propName, bool isReadOnly, IControlFactory factory) : base(ctl, propName, isReadOnly, factory)
        // Inherits from ControlMapper
        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            IControlFactory controlFactory = GetControlFactory();
            ExtendedTextBoxWin extendedTextBox = new ExtendedTextBoxWin(controlFactory);
            string propName = TestUtilsShared.GetRandomString();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            SortPropertySelectorMapper mapper = new SortPropertySelectorMapper(extendedTextBox, propName, true, controlFactory);
            //---------------Test Result -----------------------
            Assert.IsInstanceOf(typeof(IControlMapper), mapper);
            Assert.AreSame(extendedTextBox, mapper.Control);
            Assert.AreSame(extendedTextBox, mapper.ExtendedTextBox);
            Assert.AreEqual(propName, mapper.PropertyName);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.AreEqual(Color.White,extendedTextBox.TextBox.BackColor);
            Assert.AreEqual(controlFactory, mapper.ControlFactory);
            Assert.IsNotNull(mapper.BoRelationshipMapper);
            BORelationshipMapper boRelationshipMapper = mapper.BoRelationshipMapper;
            Assert.AreEqual(propName, boRelationshipMapper.RelationshipName);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_ShouldSetBusinessObjectOnRelationshipMapper()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationship);
            TestUtilsShared.AssertIsInstanceOf<SingleRelationship<DMSortOrder>>(relationship.Relationships[relationshipName]);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationship;
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreSame(relationship, mapper.BoRelationshipMapper.BusinessObject);
        }

        // Maps to single Relationship only
        [Test]
        public void Test_BusinessObject_WhenPropertyNameMapsToSingleRelationship_ShouldSetBusinessObject()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationship);
            TestUtilsShared.AssertIsInstanceOf<SingleRelationship<DMSortOrder>>(relationship.Relationships[relationshipName]);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationship;
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, mapper.BusinessObject);
        }

        // not Prop
        [Test]
        public void Test_BusinessObject_WhenPropertyNameMapsToProperty_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            const string propName = "DMSortOrderID";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(propName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationship);
            //---------------Execute Test ----------------------
            try
            {
                mapper.BusinessObject = relationship;
                Assert.Fail("Expected to throw an HabaneroDeveloperException");
            }
                //---------------Test Result -----------------------
            catch (RelationshipNotFoundException ex)
            {
                StringAssert.Contains("The relationship 'DMSortOrderID' on 'DMRelationship' cannot be found", ex.Message);
            }
        }

        // not Multiple Relationship
        [Test]
        public void Test_BusinessObject_WhenPropertyNameMapsToMultipleRelationship_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "RelationshipProperties";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relationship);
            TestUtilsShared.AssertIsInstanceOf<IMultipleRelationship>(relationship.Relationships[relationshipName]);
            //---------------Execute Test ----------------------
            try
            {
                mapper.BusinessObject = relationship;
                Assert.Fail("Expected to throw an HabaneroDeveloperException");
            }
            //---------------Test Result -----------------------
            catch (HabaneroDeveloperException ex)
            {
                StringAssert.Contains(string.Format("The relationship '{0}' on 'DMRelationship' is " +
                    "not a single relationship to a 'DMSortOrder'. " +
                    "The SortPropertySelectorMapper must be mapped to a Single Relationship to a 'DMSortOrder'.",
                    relationshipName), ex.Message);
                Assert.AreNotSame(relationship, mapper.BusinessObject);
                Assert.AreNotSame(relationship, mapper.BoRelationshipMapper.BusinessObject);
            }
        }

        // not Multiple Relationship
        [Test]
        public void Test_BusinessObject_WhenPropertyNameMapsToMultipleRelationship_WithDifferentBoType_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "Properties";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            TestUtilsShared.AssertIsInstanceOf<IMultipleRelationship>(dmClass.Relationships[relationshipName]);
            //---------------Execute Test ----------------------
            try
            {
                mapper.BusinessObject = dmClass;
                Assert.Fail("Expected to throw an HabaneroDeveloperException");
            }
            //---------------Test Result -----------------------
            catch (HabaneroDeveloperException ex)
            {
                StringAssert.Contains(string.Format("The relationship '{0}' on 'DMClass' is " +
                    "not a single relationship to a 'DMSortOrder'. " +
                    "The SortPropertySelectorMapper must be mapped to a Single Relationship to a 'DMSortOrder'.",
                    relationshipName), ex.Message);
                Assert.AreNotSame(dmClass, mapper.BusinessObject);
                Assert.AreNotSame(dmClass, mapper.BoRelationshipMapper.BusinessObject);
            }
        }

        // Single Relationship BOs must be of type DMSortOrder
        [Test]
        public void Test_BusinessObject_WhenPropertyNameMapsToSingleRelationshipNotOfTypeDMSortOrder_ShouldThrowError()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "Assembly";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dmClass);
            TestUtilsShared.AssertIsInstanceOf<SingleRelationship<DMAssembly>>(dmClass.Relationships[relationshipName]);
            //---------------Execute Test ----------------------
            try
            {
                mapper.BusinessObject = dmClass;
                Assert.Fail("Expected to throw an HabaneroDeveloperException");
            }
                //---------------Test Result -----------------------
            catch (HabaneroDeveloperException ex)
            {
                StringAssert.Contains(string.Format("The relationship '{0}' on 'DMClass' is " + 
                    "not a single relationship to a 'DMSortOrder'. " + 
                    "The SortPropertySelectorMapper must be mapped to a Single Relationship to a 'DMSortOrder'.",
                    relationshipName), ex.Message);
                Assert.AreNotSame(dmClass, mapper.BusinessObject);
                Assert.AreNotSame(dmClass, mapper.BoRelationshipMapper.BusinessObject);
            }
        }

        // Test when BO is null
        [Test]
        public void Test_BusinessObject_SetToNull()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.IsNotNull(mapper.BusinessObject);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = null;
            //---------------Test Result -----------------------
            Assert.IsNull(mapper.BusinessObject);
        }

        [Test]
        public void Test_BusinessObject_WhenSetToNull_WhenNotReadOnly_ShouldDisableButton()
        {
            //---------------Set up test pack-------------------
            const bool isReadOnly = false;
            const string relationshipName = "DMSortOrder";
            IControlFactory controlFactory = GetControlFactory();
            ExtendedTextBoxWin extendedTextBox = new ExtendedTextBoxWin(controlFactory);
            SortPropertySelectorMapper mapper = new SortPropertySelectorMapper(extendedTextBox, relationshipName, isReadOnly, controlFactory);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.AreEqual(isReadOnly, mapper.IsReadOnly);
            Assert.IsTrue(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsTrue(extendedTextBox.Button.Enabled);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsFalse(extendedTextBox.Button.Enabled);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_WhenReadOnly_ShouldDisableButton()
        {
            //---------------Set up test pack-------------------
            const bool isReadOnly = true;
            const string relationshipName = "DMSortOrder";
            IControlFactory controlFactory = GetControlFactory();
            ExtendedTextBoxWin extendedTextBox = new ExtendedTextBoxWin(controlFactory);
            SortPropertySelectorMapper mapper = new SortPropertySelectorMapper(extendedTextBox, relationshipName, isReadOnly, controlFactory);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.AreEqual(isReadOnly, mapper.IsReadOnly);
            Assert.IsFalse(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsFalse(extendedTextBox.Button.Enabled);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationship;
            //---------------Test Result -----------------------
            Assert.IsFalse(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsFalse(extendedTextBox.Button.Enabled);
        }

        [Test]
        public void Test_BusinessObject_WhenSet_WhenNotReadOnly_ShouldEnableButton()
        {
            //---------------Set up test pack-------------------
            const bool isReadOnly = false;
            const string relationshipName = "DMSortOrder";
            IControlFactory controlFactory = GetControlFactory();
            ExtendedTextBoxWin extendedTextBox = new ExtendedTextBoxWin(controlFactory);
            SortPropertySelectorMapper mapper = new SortPropertySelectorMapper(extendedTextBox, relationshipName, isReadOnly, controlFactory);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.AreEqual(isReadOnly, mapper.IsReadOnly);
            Assert.IsFalse(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsFalse(extendedTextBox.Button.Enabled);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationship;
            //---------------Test Result -----------------------
            Assert.IsTrue(extendedTextBox.Enabled);
            Assert.IsFalse(extendedTextBox.TextBox.Enabled);
            Assert.IsTrue(extendedTextBox.Button.Enabled);
        }

        [Test]
        public void Test_BusinessObject_WhenSetToBOWithDMSortOrder_ShouldUpdateControlWithDMSortOrder()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationship;
            //---------------Test Result -----------------------
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
        }

        // Test when related SortOrder is empty
        [Test]
        public void Test_BusinessObject_WhenSetToBOWithNullDMSortOrder_ShouldUpdateControlWithNull()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            DMRelationship relationshipWithNullDMSortOrder = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = relationshipWithNullDMSortOrder;
            //---------------Test Result -----------------------
            Assert.AreSame(relationshipWithNullDMSortOrder, mapper.BusinessObject);
            Assert.AreEqual("", mapper.ExtendedTextBox.TextBox.Text);
        }

        [Test]
        public void Test_BusinessObject_WhenSetToNull_ShouldUpdateControlWithNull()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
            //---------------Execute Test ----------------------
            mapper.BusinessObject = null;
            //---------------Test Result -----------------------
            Assert.IsNull(mapper.BusinessObject);
            Assert.AreEqual("", mapper.ExtendedTextBox.TextBox.Text);
        }

        // Test when related sort order changes from null
        [Test]
        public void Test_RelatedSortOrder_WhenChangedFromNull_ShouldUpdateControlWithDMSortOrderToString()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = null;
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreNotSame(dmSortOrder, relationship.DMSortOrder);
            Assert.AreEqual("", mapper.ExtendedTextBox.TextBox.Text);
            //---------------Execute Test ----------------------
            relationship.DMSortOrder = dmSortOrder;
            //---------------Test Result -----------------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
        }

        // Test when related sort order changes
        [Test]
        public void Test_RelatedSortOrder_WhenChanged_ShouldUpdateControlWithNewDMSortOrderToString()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            DMSortOrder dmSortOrder2 = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder2.ToString()));
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
            //---------------Execute Test ----------------------
            relationship.DMSortOrder = dmSortOrder2;
            //---------------Test Result -----------------------
            Assert.AreSame(dmSortOrder2, relationship.DMSortOrder);
            Assert.AreEqual(dmSortOrder2.ToString(), mapper.ExtendedTextBox.TextBox.Text);
        }

        // Test when related sort order changes to null
        [Test]
        public void Test_RelatedSortOrder_WhenChangedToNull_ShouldUpdateControlWithNull()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.AreSame(dmSortOrder, relationship.DMSortOrder);
            Assert.IsFalse(String.IsNullOrEmpty(dmSortOrder.ToString()));
            Assert.AreSame(relationship, mapper.BusinessObject);
            Assert.AreEqual(dmSortOrder.ToString(), mapper.ExtendedTextBox.TextBox.Text);
            //---------------Execute Test ----------------------
            relationship.DMSortOrder = null;
            //---------------Test Result -----------------------
            //Assert.IsNull(relationship.DMSortOrder);
            Assert.AreEqual("", mapper.ExtendedTextBox.TextBox.Text);
        }

        [Test]
        public void Test_SetPropertiesCollection()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrderWith3Props();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            IDMClass relatedClass = relationship.RelatedClass;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(relatedClass);
            //---------------Execute Test ----------------------
            BusinessObjectCollection<DMProperty> properties = relatedClass.Properties;
            mapper.DMProperties = properties;
            //---------------Test Result -----------------------
            Assert.AreSame(mapper.DMProperties, properties);
        }

        [Test]
        public void Test_CreatePopupForm_FormShouldBeSetupCorrectly()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrder();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            var relatedClass = relationship.RelatedClass;
            BusinessObjectCollection<DMProperty> properties = relatedClass.Properties;
            Assert.AreEqual(2, properties.Count);
            TestUtilsShared.CreateUnsavedValidDMProperty(relatedClass);
            TestUtilsShared.CreateUnsavedValidDMProperty(relatedClass);
            mapper.DMProperties = properties;
            //---------------Assert Precondition----------------
            Assert.AreEqual(4, properties.Count);
            Assert.IsNotNull(mapper.DMProperties);
            //---------------Execute Test ----------------------
            mapper.CreatePopupForm();
            //mapper.ShowPopupform();
            //---------------Test Result -----------------------
            IFormHabanero popupForm = mapper.PopupForm;
            Assert.IsNotNull(popupForm);
            Assert.AreEqual(1, popupForm.Controls.Count);
            Assert.IsInstanceOf(typeof (IEditableGridControl), popupForm.Controls[0]);
            IEditableGridControl editableGridControl = (IEditableGridControl) popupForm.Controls[0];
            //Assert.IsTrue(editableGridControl.Buttons.Visible);
            //Assert.IsFalse(editableGridControl.Buttons["Save"].Visible);
            //Assert.IsTrue(editableGridControl.Buttons["Cancel"].Visible);
            Assert.AreEqual(3, editableGridControl.Grid.Columns.Count);
            Assert.IsInstanceOf(typeof(IDataGridViewComboBoxColumn), editableGridControl.Grid.Columns[1]);
            Assert.IsInstanceOf(typeof(IDataGridViewComboBoxColumn), editableGridControl.Grid.Columns[2]);
            IDataGridViewComboBoxColumn propertyNameColumn =
            (IDataGridViewComboBoxColumn)editableGridControl.Grid.Columns[1];
            Assert.AreEqual(4, propertyNameColumn.Items.Count);

            IDataGridViewComboBoxColumn sortDirectionColumn =
                (IDataGridViewComboBoxColumn) editableGridControl.Grid.Columns[2];
            Assert.AreEqual(3, sortDirectionColumn.Items.Count);
        }

        [Test]
        public void Test_CreatePopupForm_ShouldThrowError_IfDMPropertiesHasNotBeenSet()
        {
            //---------------Set up test pack-------------------
            const string relationshipName = "DMSortOrder";
            SortPropertySelectorMapper mapper = CreateSortPropertySelectorMapper(relationshipName);
            DMRelationship relationship = TestUtilsDMRelationship.CreateUnsavedValidDMRelationship();
            DMSortOrder dmSortOrder = TestUtilsDMSortOrder.CreateUnsavedValidDMSortOrder();
            relationship.DMSortOrder = dmSortOrder;
            mapper.BusinessObject = relationship;
            //---------------Assert Precondition----------------
            Assert.IsNull(mapper.DMProperties);
            //---------------Execute Test ----------------------
            try
            {
                mapper.CreatePopupForm();
                Assert.Fail("Expected to throw an a HabaneroDeveloperException");
            }
            //---------------Test Result -----------------------
            catch (HabaneroDeveloperException ex)
            {
                StringAssert.Contains("The DMProperties must be set first", ex.Message);
            }
 
        }


        
        // Test Control Value changes, should update the relationship (most prob when the value changes from null to something)

        // Do the SortPosition like the properties of the DMClass

        private static SortPropertySelectorMapper CreateSortPropertySelectorMapper(string relationshipName)
        {
            IControlFactory controlFactory = GetControlFactory();
            ExtendedTextBoxWin extendedTextBox = new ExtendedTextBoxWin(controlFactory);
            SortPropertySelectorMapper mapper = new SortPropertySelectorMapper(extendedTextBox, relationshipName, true, controlFactory);
            return mapper;
        }

    }
}
// ReSharper restore ClassWithVirtualMembersNeverInherited.Global
